# coding=utf-8
# !/usr/bin/python3
import re
from lxml import etree

import requests

cookies = {
    'uuid_n_v': 'v1',
    'uuid': '1B56E070620D11EF8BA6E5594D7FF2C37AA7608BF8634FAF96EF0429F952584B',
    '_csrf': 'd9ac4c5b80671aef348bd2d581c493dfdf1820a9cfc82afb90a8f29a5314a48c',
    '_lx_utm': 'utm_source%3Dbing%26utm_medium%3Dorganic',
    '_lxsdk_cuid': '191842c75bcc8-016ec2e1fc6309-26001151-1fa400-191842c75bcb1',
    '_lxsdk': '1B56E070620D11EF8BA6E5594D7FF2C37AA7608BF8634FAF96EF0429F952584B',
    'Hm_lvt_703e94591e87be68cc8da0da7cbd0be2': '1724499392',
    'HMACCOUNT': 'ED0735D1CE0FB96D',
    'Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2': '1724499409',
    '__mta': '244216122.1724499392264.1724499398947.1724499410154.3',
    '_lxsdk_s': '191842c75bd-689-d0b-647%7C%7C6',
}

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    # 'Cookie': 'uuid_n_v=v1; uuid=1B56E070620D11EF8BA6E5594D7FF2C37AA7608BF8634FAF96EF0429F952584B; _csrf=d9ac4c5b80671aef348bd2d581c493dfdf1820a9cfc82afb90a8f29a5314a48c; _lx_utm=utm_source%3Dbing%26utm_medium%3Dorganic; _lxsdk_cuid=191842c75bcc8-016ec2e1fc6309-26001151-1fa400-191842c75bcb1; _lxsdk=1B56E070620D11EF8BA6E5594D7FF2C37AA7608BF8634FAF96EF0429F952584B; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1724499392; HMACCOUNT=ED0735D1CE0FB96D; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1724499409; __mta=244216122.1724499392264.1724499398947.1724499410154.3; _lxsdk_s=191842c75bd-689-d0b-647%7C%7C6',
    'Pragma': 'no-cache',
    'Referer': 'https://www.maoyan.com/films',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Google Chrome";v="128"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'showType': '2',
}

response = requests.get('https://www.maoyan.com/films', params=params, cookies=cookies, headers=headers)
data = response.text
# 下载woff文件
    # 正则获取woff下载路径
path = re.findall(',url\("(.*?woff)"', data)[0]
    # 拼接woff文件地址
woff_url = "https:" + path
     # 下载文件
res = requests.get(woff_url)
    # woff文件存本地
with open("./font.woff", "wb") as f:
    f.write(res.content)

#解密woff文件
from utils import extract_text_from_font

font_file_path = "./font.woff"
code=extract_text_from_font(font_file_path)
for k,v in code.items():

    data=data.replace(k+";", v) #此处一定要重新赋值  不然还是打印这之前的data


#etree解析页面
html_Data = etree.HTML(data)

div_list = html_Data.xpath('//dl[@class="movie-list"]//dd') #定位到dd  也就是所有的信息标签下


list={}
for v in div_list:
    try:
        title = v.xpath(f'./div[2]/a/text()')[0]   #在dd的条件下找到电影名称
        stone_font = v.xpath(f'./div[3]/span/text()')[0]  #在dd的条件下找到想看人数标签

        list[title] = stone_font
    except IndexError:
        continue
print(list)